package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.dal.entitys.Goods;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 创建日期: 2022/05/19 14:41
 *
 * @author yangfan
 */
@RestController
public class ProductController {

    @Autowired
    IProductService iProductService;

    /**
     * 查看商品明细
     * @param id
     * @return
     */
    @GetMapping("/shopping/product/{id}")
    public ResponseData getProductDetail(@PathVariable(value = "id") Long id){
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(id);

        // 根据业务逻辑处理请求
        ProductDetailResponse productDetail = iProductService.getProductDetail(productDetailRequest);

        if (ShoppingRetCode.SUCCESS.getCode().equals(productDetail.getCode())) {
            // 查询成功
            ProductDetailDto result = productDetail.getProductDetailDto();
            return new ResponseUtil().setData(result);
        }

        // 查询失败
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());

    }

    /**
     * 分页查询商品列表
     * @return
     */
    @GetMapping("/shopping/goods")
    public ResponseData getAllProduct(AllProductRequest allProductRequest) {

        AllProductResponse allProduct = iProductService.getAllProduct(allProductRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(allProduct.getCode())) {
            List<ProductDto> productDtoList = allProduct.getProductDtoList();
            Long total = allProduct.getTotal();
            Goods goods = new Goods();
            goods.setData(productDtoList);
            goods.setTotal(total);
            return new ResponseUtil().setData(goods);
        }

        return new ResponseUtil().setErrorMsg(allProduct.getMsg());
    }


    @GetMapping("/shopping/recommend")
    public ResponseData getRecommendGoods () {

        RecommendResponse recommendGoods = iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(recommendGoods.getCode())) {
            Set<PanelDto> result = recommendGoods.getPanelContentItemDtos();
            return new ResponseUtil().setData(result);
        }

        return new ResponseUtil().setErrorMsg(recommendGoods.getMsg());
    }




}
